package com.jsgt.greekfire.ui.trading.delegation

import android.content.Context
import android.content.Intent
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.fhx.core.base.BaseActivity
import com.jsgt.greekfire.R
import com.jsgt.greekfire.factory.CoinViewModelFactory
import com.jsgt.greekfire.ui.trading.adapter.DelegationAdapter
import com.jsgt.greekfire.viewmodel.CoinViewModel
import kotlinx.android.synthetic.main.activity_delegation.*
import kotlinx.android.synthetic.main.view_nav_single_ch.*

/**
 * 委托
 * author fhx
 * create time 2020/5/1 13:29
 **/
class DelegationActivity : BaseActivity() {
    private val coinViewModel: CoinViewModel by lazy {
        ViewModelProvider(
            this,
            CoinViewModelFactory()
        ).get(CoinViewModel::class.java)
    }

    companion object {
        private const val TYPE = "type"
        const val TYPE_CURRENT = "1"
        const val TYPE_HISTORY = "2"
        fun start(context: Context, type: String) {
            val intent = Intent(context, DelegationActivity::class.java)
            intent.putExtra(TYPE, type)
            context.startActivity(intent)
        }
    }

    override fun getLayoutId(): Int = R.layout.activity_delegation

    override fun main() {
        nav_rb_1.text = getString(R.string.current_delegation)
        nav_rb_2.text = getString(R.string.history_delegation)
        val type = intent.getStringExtra(TYPE)
        nav_rb_1.setOnCheckedChangeListener { _, isChecked ->
            if (isChecked) {
                coinViewModel.currentSaleList()
            } else {
                coinViewModel.historySaleList()
            }
        }
        if (type == TYPE_CURRENT) {
            nav_rb_1.isChecked = true
        } else {
            nav_rb_2.isChecked = true
        }
        val delegationAdapter =
            DelegationAdapter()
        delegation_rv.adapter = delegationAdapter
        coinViewModel.currentSaleList()
        coinViewModel.delegationBeanListLiveData.observe(this, Observer {
            delegationAdapter.refreshData(it)
        })
        coinViewModel.delegationHistoryLiveData.observe(this, Observer {
            delegationAdapter.refreshData(it)
        })
    }
}